package com.zy.xuanwo.acitivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zy.xuanwo.R;
import com.zy.xuanwo.bean.DeviceStatusBean;
import com.zy.xuanwo.utils.MD5Utils;
import com.zy.xuanwo.utils.UserInfoSPManager;
import com.zy.xuanwo.view.CleanEditText;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.fogcloud.easylink.api.EasyLink;
import io.fogcloud.easylink.helper.EasyLinkCallBack;
import io.fogcloud.easylink.helper.EasyLinkParams;
import io.fogcloud.easylink.wifiman.EasyLinkWifiManager;
import io.fogcloud.fog_mdns.api.MDNS;
import io.fogcloud.fog_mdns.helper.SearchDeviceCallBack;

/**
 * @功能 配置wifi
 * Created by njb on 2016/7/7 0007.
 */
public class ConfigurationWiFiActivity extends AppCompatActivity implements View.OnClickListener {

    @Bind(R.id.configuration_btn)
    Button configurationBtn;
    @Bind(R.id.left_sliding_menu_iv)
    ImageView mBackView;
    @Bind(R.id.edt_wifissid)
    CleanEditText edtWifissid;
    @Bind(R.id.edt_wifipsw)
    TextView edtWifipsw;
    private Context ctx = null;
    private EasyLinkWifiManager mWifiManager = null;
    private EditText wifissid;
    private EditText wifipsw;
    private boolean startTag = false;
    private String devip;
    private DeviceStatusBean mdeviceStatus;
    private String userToken;
    private String bindKey;
    private int devid;
    private int  Port = 8000;
    public EasyLink elink;

    private MyHandler myhandler;
    private String TAG = "---ey 2.0 ";
    private MDNS mdns;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_wifi);
        ButterKnife.bind(this);
        elink = new EasyLink(ConfigurationWiFiActivity.this);
        mdns = new MDNS(ConfigurationWiFiActivity.this);
        wifissid.setText(elink.getSSID());

        myhandler = new MyHandler();


    }


    @OnClick({R.id.configuration_btn, R.id.left_sliding_menu_iv})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_sliding_menu_iv:
                startActivity(new Intent(ConfigurationWiFiActivity.this,SocketActivity.class));
                this.finish();
                break;
            case R.id.configuration_btn:
                startWiFi();
                startmDNS();
                break;
            default:
                break;
        }
    }


    private void startmDNS() {
            String serviceInfo = "_easylink._tcp.local.";

        Message msg = new Message();
        msg.what = 1;
        msg.obj = "\r\n 正在打开mDNS";
        myhandler.sendMessage(msg);

        mdns.startSearchDevices(serviceInfo,
                new SearchDeviceCallBack() {
                    @Override
                    public void onDevicesFind(JSONArray deviceStatus) {
                            if (deviceStatus != null && !deviceStatus.toString().equals("[]")) {
                                Log.e("device1", deviceStatus.toString());
                                java.lang.reflect.Type type = new TypeToken<List<DeviceStatusBean>>() {}.getType();
                                List<DeviceStatusBean> deviceStatusBeanList = new Gson().fromJson(deviceStatus.toString(),type);
                                Log.e("device2",""+ deviceStatusBeanList.get(0).toString());
                                devip = deviceStatusBeanList.get(0).getDeviceIP();
                                bindingDev();
                                Log.e("device3",""+ devip);
                                mdns = null;
                            }
                    }

                    @Override
                    public void onFailure(int code, String message) {
                        Message msg = new Message();
                        msg.what = 1;
                        msg.obj = message.toString() + "\r\n";
                        myhandler.sendMessage(msg);
                    }

                    @Override
                    public void onSuccess(String message) {
                        Message msg = new Message();
                        msg.what = 1;
                        msg.obj = message.toString() + "\r\n";
                        myhandler.sendMessage(msg);

                    }
                });
    }



    private void bindingDev() {
        if(userToken != null) {
            userToken = UserInfoSPManager.getUserToken();
            bindKey = MD5Utils.MD5Encode(devip + userToken);
//           bindService(devid,Port,bindKey)
        }


    }




    private void startWiFi() {
        EasyLinkParams easylinkPara = new EasyLinkParams();
        easylinkPara.ssid = wifissid.getText().toString().trim();
        easylinkPara.password = wifipsw.getText().toString().trim();
        easylinkPara.runSecond = 20000;
        easylinkPara.extraData = "xu sin";
        elink.startEasyLink(easylinkPara, new EasyLinkCallBack() {
            @Override
            public void onSuccess(int code, String message) {
                Log.e(TAG, code + message);
                Message msg = new Message();
                msg.what = 1;
                msg.obj = "code =  " + code + ", message = " + message;
                myhandler.sendMessage(msg);
            }

            @Override
            public void onFailure(int code, String message) {
                Log.d(TAG, code + message);
                Message msg = new Message();
                msg.what = 1;
                msg.obj = "code =  " + code + ", message = " + message;
                myhandler.sendMessage(msg);
            }
        });
    }

    class MyHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

        }
    }
   /* elink.stopEasyLink(new EasyLinkCallBack() {

        @Override
        public void onSuccess(int code, String message) {
            Log.d(TAG, code + message);
            Message msg = new Message();
            msg.what = 1;
            msg.obj = "code =  " + code + ", message = " + message;
            myhandler.sendMessage(msg);
        }

        @Override
        public void onFailure(int code, String message) {
            Log.d(TAG, code + message);
            Message msg = new Message();
            msg.what = 1;
            msg.obj = "code =  " + code + ", message = " + message;
            myhandler.sendMessage(msg);
        }
    });*/
}
